//
//  ZCMiddleView.m
//  BaiSi
//
//  Created by 张灿 on 16/5/3.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCMiddleView.h"
#import <UIImageView+WebCache.h>
#import "ZCThemeItem.h"
#import <DACircularProgress/DALabeledCircularProgressView.h>
#import "ZCSeeBigPictureViewController.h"

@interface ZCMiddleView ()

@property (weak, nonatomic) IBOutlet UIImageView *pictureView;

@property (weak, nonatomic) IBOutlet UIImageView *gifView;

@property (weak, nonatomic) IBOutlet UIButton *seeBig;

@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *progressView;

@property (weak, nonatomic) UIImage *image;

@end

@implementation ZCMiddleView

+ (instancetype)middleView
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}

- (void)setItem:(ZCThemeItem *)item
{
    _item = item;
    
    [_pictureView sd_setImageWithURL:[NSURL URLWithString:item.image0] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        if (expectedSize < 0) {
            return ;
        }
        CGFloat progress = 1.0 * receivedSize / expectedSize;
        self.progressView.progress = progress;
        self.progressView.roundedCorners = 10;
        self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.1f%%", progress * 100];
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.image = image;
        
    }];
    
    _gifView.hidden = !item.is_gif;
    
    _seeBig.hidden = !item.is_bigPicture;
    
    //判断是不是大图
    if (item.is_bigPicture) {
        _pictureView.contentMode = UIViewContentModeTop;
        _pictureView.clipsToBounds = YES;
    } else {
        _pictureView.contentMode = UIViewContentModeScaleToFill;
        _pictureView.clipsToBounds = NO;
    }
}

//点击查看大图
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //创建一个uiviewcontroller
    ZCSeeBigPictureViewController *seeBigPictureVC = [[ZCSeeBigPictureViewController alloc] init];
    seeBigPictureVC.item = self.item;
    
    //显示
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:seeBigPictureVC animated:YES completion:nil];
}

@end
